package com.yd.controller.sys;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yd.pojo.Abts;
import com.yd.service.AbtsService;
import com.yd.tool.UtsReg;

@Controller
public class AbtsControllerSys {
	@Resource
	private AbtsService abtsService;
	
	@RequestMapping(value={"/sys/abts/list"})
	@ResponseBody
	public String sys_abts_list(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject();
		
		String so="";
		String tid="";
		String page="";
		String size="";
		if(UtsReg.getStringKB(request.getParameter("so"))){so=request.getParameter("so").trim();}
		if(UtsReg.getIntXl(request.getParameter("page"))){page=request.getParameter("page").trim();}else{page = "1";}
		if(UtsReg.getIntXl(request.getParameter("size"))){size=request.getParameter("size").trim();}else{size = "10";}
		if(UtsReg.getIntXl(request.getParameter("tid"))){
			tid=request.getParameter("tid").trim();
		}
		
		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));
		List<Abts> list = null;
		EntityWrapper<Abts> ew=new EntityWrapper<Abts>();
		if(so.length()>0){
			ew.like("title", so);
		}
		if(tid.length()>0){
			ew.eq("tid", tid);
		}
		list=abtsService.selectList(ew.orderBy("id",false));
		PageInfo<Abts> pages = new PageInfo<Abts>(list);
		
		jsonObject.put("data", pages);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();
		
	}
	
	@RequestMapping(value={"/sys/abts/add"})
	@ResponseBody
	public String sys_abts_add(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject(); 
		
		String title="";
		String tourlname="";
		String doc="";
		String img="";
		String info="";
		String url="";
		String tid="";
		
		if(UtsReg.getStringKB(request.getParameter("title"))){title=request.getParameter("title").trim();}else{jsonObject.put("msg", "名称  错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getStringKB(request.getParameter("doc"))){doc=request.getParameter("doc").trim();}else{jsonObject.put("msg", "描述  错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getStringKB(request.getParameter("img"))){img=request.getParameter("img").trim();}
		if(UtsReg.getStringKB(request.getParameter("url"))){url=request.getParameter("url").trim();}
		if(UtsReg.getStringKB(request.getParameter("info"))){info=request.getParameter("info").trim();}
		if(UtsReg.getStringKB(request.getParameter("tourlname"))){tourlname=request.getParameter("tourlname").trim();}else{jsonObject.put("msg", "唯一访问标示 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getInt(request.getParameter("tid"))){tid=request.getParameter("tid").trim();}
		
		if(abtsService.selectCount(new EntityWrapper<Abts>().eq("tourlname", tourlname))>0){
			jsonObject.put("msg", "唯一访问标示被占用");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		Abts abts = new Abts();
		abts.setTitle(title);
		abts.setTourlname(tourlname);
		abts.setDoc(doc);
		abts.setImg(img);
		abts.setInfo(info);
		abts.setUrl(url);
		abts.setTid(tid);
		if(!abtsService.insert(abts)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		
		return jsonObject.toString();
	}
	
	@RequestMapping(value={"/sys/abts/upbyid"})
	@ResponseBody
	public String sys_abts_upbyid(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject(); 
		
        String id="";
		String title="";
		String tourlname="";
		String doc="";
		String img="";
		String info="";
		String url="";
		String tid="";
		
		if(UtsReg.getIntXl(request.getParameter("id"))){id=request.getParameter("id").trim();}else{jsonObject.put("msg", "参数  错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getStringKB(request.getParameter("title"))){title=request.getParameter("title").trim();}else{jsonObject.put("msg", "名称  错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getStringKB(request.getParameter("doc"))){doc=request.getParameter("doc").trim();}else{jsonObject.put("msg", "描述  错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getStringKB(request.getParameter("img"))){img=request.getParameter("img").trim();}
		if(UtsReg.getStringKB(request.getParameter("info"))){info=request.getParameter("info").trim();}
		if(UtsReg.getStringKB(request.getParameter("url"))){url=request.getParameter("url").trim();}
		if(UtsReg.getStringKB(request.getParameter("tourlname"))){tourlname=request.getParameter("tourlname").trim();}else{jsonObject.put("msg", "唯一访问标示 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getInt(request.getParameter("tid"))){tid=request.getParameter("tid").trim();}
		
		if(abtsService.selectCount(new EntityWrapper<Abts>().eq("tourlname", tourlname).ne("id", id))>0){
			jsonObject.put("msg", "唯一访问标示被占用");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		Abts abts = new Abts();
		abts.setId(Integer.valueOf(id));
		abts.setTitle(title);
		abts.setTourlname(tourlname);
		abts.setDoc(doc);
		abts.setImg(img);
		abts.setInfo(info);
		abts.setUrl(url);
		abts.setTid(tid);
		
		if(!abtsService.updateById(abts)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		
		return jsonObject.toString();
	}
	
	@RequestMapping(value={"/sys/abts/delbyid"})
	@ResponseBody
	public String sys_abts_delbyid(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject(); 
		
		String id="";
		if(UtsReg.getIntXl(request.getParameter("id"))){id=request.getParameter("id").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		if(abtsService.selectCount(new EntityWrapper<Abts>().eq("id", id))<1){
			jsonObject.put("msg", "数据不存在");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		if(!abtsService.deleteById(id)){
			jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
	}
}

